//
//  MineViewController.m
//  MJApp
//
//  Created by scjh on 15/3/26.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeadTableViewCell.h"
#import "MineCellTableViewCell.h"

#import "MineInfoViewController.h"
#import "MineCashViewController.h"
#import "MineAddressViewController.h"
#import "MineOrderViewController.h"
#import "MineMessageTableViewController.h"
#import "MinePassViewController.h"
#import "MineAboutTableViewController.h"
#import "LoginAndRegisterViewController.h"
#import "ShopProductAddCheckImageViewController.h"

#import "NSDictionary+Category.h"
#import "DynamicViewController.h"
#import "MineInfoNewViewController.h"
#import "FindPasswordViewController.h"


@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate,HomeCellDelegate>

@property(nonatomic,strong)UITableView *mineTableView;
@property(nonatomic,strong)NSMutableArray *tableViewArray;
@property(nonatomic,strong)NSDictionary *userInfoDic;

@end

@implementation MineViewController

- (NSMutableArray *)tableViewArray {
    
    if (!_tableViewArray) {
        
        _tableViewArray = [NSMutableArray array];
    }
    return _tableViewArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self Data];
    [self UI];
    [self NaviUI];
   

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginSuccess:)
                                                 name:K_NOTICE_MINE
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeImage:)
                                                 name:K_Image_Change
                                               object:nil];
   
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];

    if([userInfoTool share].ID) {
        
        [self obtaionData];
    }
    
}

- (void)loginSuccess:(NSNotification *)notification {
    
    [self refreshUserInfo];
}


- (void)refreshUserInfo {
    
   
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.mineTableView reloadRowsAtIndexPaths:@[indexPath]
                              withRowAnimation:UITableViewRowAnimationFade];
}

- (void)Data {
    
    NSArray *nameArray = @[@[@"",@"我的订单",@"我的钱包",@"我的地址"],
                           @[@"我的消息",@"我的动态"],
                           @[@"密码锁记录",@"修改账号密码"]];
    NSArray *imageArray = @[@[@"",@"grzx11_11.png",@"grzx11_14.png",@"grzx11_16.png"],
                           @[@"grzx11_18.png",@"grzx11_20.png"],
                           @[@"grzx11_22.png",@"xg_20.png"]];
    int num = 0;
    for (int i = 0; i < 3; i ++) {
        
        if (i == 0) {
            num = 4;
        }else if (i == 1) {
            num = 2;
        }else {
            num = 2;
        }
        NSMutableArray *array = [NSMutableArray array];
        for (int j = 0; j < num; j ++) {
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            dic[@"name"] = nameArray[i][j];
            dic[@"imagestr"] = imageArray[i][j];
            [array addObject:dic];
        }
        [self.tableViewArray addObject:array];
    }
    
//    self.tableViewArray = @[
//                            @[@{@"name":@"",@"imagestr":@""},
//                              @{@"name":@"我的订单",@"imagestr":@"grzx11_11.png"},
//                              @{@"name":@"我的钱包",@"imagestr":@"grzx11_14.png"},
//                              @{@"name":@"我的地址",@"imagestr":@"grzx11_16.png"}],
//                            @[@{@"name":@"我的消息",@"imagestr":@"grzx11_18.png"},
//                              @{@"name":@"我的动态",@"imagestr":@"grzx11_20.png"}],
//                            @[@{@"name":@"密码锁记录",@"imagestr":@"grzx11_22.png"}]
//                            ];
}

- (void)obtaionData {
    
    NSDictionary *dic = @{@"user_id":[userInfoTool share].ID};
    [WLHttpRequestAfter userCenterInfoWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            self.userInfoDic = [resultDictionary[@"data"] nullValueToEmpty];
            self.tableViewArray[1][0][@"num"] = resultDictionary[@"data"][@"all_count"];
            [self.mineTableView reloadData];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

#pragma mark 搭建UI界面
- (void)UI{
    
    
    self.title = @"我";
    CGRect bounds = self.view.bounds;
    bounds.size.height -= 64;
    self.mineTableView = [[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped];
    self.mineTableView.separatorColor = [UIColor clearColor];
    self.mineTableView.backgroundColor = Color_Back_Black;
    self.mineTableView.dataSource = self;
    self.mineTableView.delegate = self;
    [self.view addSubview:self.mineTableView];
}

- (void)NaviUI {
    
    [self.rightBtn setTitle:@"设置" forState:UIControlStateNormal];
}

- (void)clickRightButton {
    
    [self.navigationController pushViewController:[[MineAboutTableViewController alloc] init] animated:YES];
}


#pragma mark tableview的代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.tableViewArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.tableViewArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        MineHeadTableViewCell *cell = (MineHeadTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;

    }
    MineCellTableViewCell *cell = (MineCellTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        MineHeadTableViewCell *cell = [MineHeadTableViewCell initWithTableView:tableView];
        cell.delegate = self;
        return cell;
    }
    MineCellTableViewCell *cell = [MineCellTableViewCell initWithTableView:tableView];
    cell.infoDic = self.tableViewArray[indexPath.section][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(![userInfoTool share].ID) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        HWNavigationController *naviVC = [[HWNavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naviVC animated:YES completion:nil];
        return;
    }
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        MineInfoNewViewController *infoVC = [[MineInfoNewViewController alloc] init];
        infoVC.title = @"个人中心";
        [self.navigationController pushViewController:infoVC animated:YES];
        
    }else if(indexPath.section == 0 && indexPath.row == 2){
        
        [self.navigationController pushViewController:[[MineCashViewController alloc] initWithDic:self.userInfoDic] animated:YES];
    }else if(indexPath.section == 0 && indexPath.row == 3) {
        
        [self.navigationController pushViewController:[[MineAddressViewController alloc] init] animated:YES];
    }else if(indexPath.section == 0 && indexPath.row == 1) {
        
        [self.navigationController pushViewController:[[MineOrderViewController alloc] init] animated:YES];
    }else if(indexPath.section == 1 && indexPath.row == 0) {
        
        [self.navigationController pushViewController:[[MineMessageTableViewController alloc] initWithDic:self.userInfoDic] animated:YES];
    }else if(indexPath.section == 1 && indexPath.row == 1) {
        
        DynamicViewController *dynamicVC = [[DynamicViewController alloc] initWithDic:@{@"id":[userInfoTool share].ID}];
        dynamicVC.title = @"我的动态";
        [self.navigationController pushViewController:dynamicVC animated:YES];
    }else if(indexPath.section == 2 && indexPath.row == 0) {
        
        [self.navigationController pushViewController:[[MinePassViewController alloc] init] animated:YES];
    }else if(indexPath.section == 2 && indexPath.row == 1) {
        
        FindPasswordViewController *findVC = [[FindPasswordViewController alloc] init];
        findVC.title = @"修改密码";
        [self.navigationController pushViewController:findVC animated:YES];
    }
}

- (void)homeCell:(id)cell withInfo:(id)info {
    
    UIButton *btn = info;
    if (btn.tag == 1) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        HWNavigationController *naviVC = [[HWNavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naviVC animated:YES completion:nil];
    }else if (btn.tag == 2) {
        
        MineHeadTableViewCell *headCell = cell;
        UIImage *image = headCell.headImageView.image;
        ShopProductAddCheckImageViewController *checkImageVC = [[ShopProductAddCheckImageViewController alloc] initWithImage:image];
        [self presentViewController:checkImageVC animated:YES completion:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_NOTICE_MINE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_Image_Change object:nil];
}

- (void)changeImage:(NSNotification *)notice {
    
    UIImage *image = notice.userInfo[@"image"];
    NSDictionary *dic = @{
                          @"user_id":[userInfoTool share].ID,
                          @"type":@"0",
                          @"filename":image
                          };
    [WLHttpRequestAfter userModifyHeadWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"修改成功"];
            
            NSMutableDictionary * dic = [[[NSUserDefaults standardUserDefaults] objectForKey:userInf] mutableCopy];
            dic[@"head"] = resultDictionary[@"data"][@"url"];
            [userInfoTool updateInfo:dic];
            [self.mineTableView reloadData];
            
        }else {
            
            
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];

}

@end
